/*
 * @Author: jianglijun 
 * @Date: 2019-04-20 21:29:12 
 * @Last Modified by: jianglijun
 * @Last Modified time: 2019-04-24 23:27:52
 */
 //      CONTROLLERTS
let path = require("path");
let { addMusicdb, updateMusicDb, deletMusic, getMusicInfor} = require("../models/music.js");
let { isContained  } = require("../utils/utils.js");
function commFuntions(ctx){
    let {title,singer,time,uid, type} = ctx.request.body;
    if(!title){
        return {
            errno:4,
            mes:"标题,歌手,时间,uid不能为空1"
        }
    }
    if(!singer){
        return {
            errno:4,
            mes:"标题,歌手,时间,uid不能为空2"
        }
    }
    if(!time){
        return {
            errno:4,
            mes:"标题,歌手,时间,uid不能为空3"
        }
    }
    if(!ctx.session.user){
        return {
            errno:4,
            mes:"标题,歌手,时间,uid不能为空4"
        }
    }
    let {file,filelrc} = ctx.request.files;
    let file1 = ctx.request.body.file;
    let filelrc1 = ctx.request.body.filelrc;
    // console.log(!file1);
    if( (!file) && (!file1) ){
        
        return {
            errno:4,
            mes:"歌曲不能为空"
        };
    }
    console.log("c**********************************");
    let q1 = [...Object.keys(ctx.request.body),...Object.keys(ctx.request.files)];
    let flag = isContained(["title","singer","time","file","filelrc","type","uid","mid"],q1,false);
    if(!flag){
        ctx.body= {
            errno:4,
            mes:"参数不正确1"
        }
        return;
    }
    let paramObj = {
        title,
        singer,
        time:parseInt(time+""),
        file:null,
        filelrc:null,
        type:1,
        uid:ctx.session.user.id
    }
    if(!file){
        paramObj.file = file1;
    }else{
        paramObj.file = "/public/files/"+path.parse(file.path).base;
    }
    paramObj.filelrc = null;
    if(type){
        paramObj.type = +type;
    }
    if(filelrc){
        paramObj.filelrc  = "/public/files/"+path.parse(filelrc.path).base
    }else{
        paramObj.filelrc = filelrc1;
    }
    return (paramObj)

}
module.exports = {
    
    /************************** 增加音乐信息  *******************/
    async addMusic(ctx,next){
        let paramObj = commFuntions(ctx);
        if( paramObj.errno >= 0 || paramObj.mes ){
            ctx.body = {
                errno:paramObj.errno,
                mes:paramObj.mes
            }
            return;
        }
        try {
            let { affectedRows, insertId } = await addMusicdb(paramObj);
            if(affectedRows>0){
                ctx.body = {
                    errno:0,
                    msg:"插入成功"
                }
            }else {
                ctx.body = {
                    errno:10,
                    msg:"插入失败"
                }
            }
        } catch (error) {
            ctx.throw(error);
        }
    },
    
    /***************** 更新音乐信息 ******************** */
    async updateMusic(ctx,next) {
        let paramObj = await commFuntions(ctx);
        if( paramObj.errno >= 0 || paramObj.mes ){
            ctx.body = {
                errno:paramObj.errno,
                mes:paramObj.mes
            }
            return;
        }
        let {mid} = ctx.request.body;

        if(!mid){
            ctx.body = {
                errno:4,
                mes:"mid不能为空"
            }
            return;
        }
        mid = +mid;
        
        try {
            let {affectedRows,insertId} = ( await updateMusicDb(Object.assign(paramObj,{mid})) );
            if(affectedRows>=1&&insertId==0){
                ctx.body = {
                    errno:0,
                    msg:"更新成功",
                }
            }else{
                ctx.body = {
                    errno:0,
                    msg:"更新失败",
                }
            }
        } catch (error) {
            ctx.throw(error);
        }
    },

    /***************** 删除音乐 ******************** */
    async delMusic(ctx,next) {
       let {uid,mid} = ctx.params;
       try {
            let {affectedRows,insertId} = await deletMusic(uid,mid);
            if(affectedRows>0 && insertId==0){
                ctx.body = {
                    errno:0,
                    msg:"删除成功",
                }
                return;
            }     
            ctx.body = {
                errno:0,
                msg:"删除失败",
            }    
       } catch (error) {
            ctx.throw(error)           
       }
    },
    /******************* 根据mid uid 获取音乐信息 ********************* */
    async getMusicData(ctx,next) {
       let {mid} = ctx.params;
       let uid = ctx.session.user.id;
    //    console.log(mid,uid);
       try {
            let dataMsg = await getMusicInfor(+uid,+mid);    
            if(dataMsg.length>0){
                ctx.body = {
                error:0,
                msg:"成功",
                data:dataMsg[0]
                }
            }else{
                ctx.body = {
                    errno:101,
                    msg:"传递给服务器的参数不正确"
                }
            }   
       } catch (error) {
           ctx.throw(error)
       }
    }
}